﻿namespace Sidvall.UI.Wpf
{
    public static class WindowManager
    {
        #region Public Members

        #region AddControlToWindow

        public static bool AddControlToWindow(System.Windows.UIElement control, object owner)
        {
            System.Windows.Window windowOwner;
            System.Windows.DependencyObject dependencyObject;

            if (control == null)
                throw new System.ArgumentNullException("control");
            if (owner == null)
                return false;

            windowOwner = owner as System.Windows.Window;
            if (windowOwner != null)
                return AddChildToParentControl(control, windowOwner);

            dependencyObject = owner as System.Windows.DependencyObject;
            if (dependencyObject != null)
                return AddChildToParentControl(control, System.Windows.Window.GetWindow(dependencyObject));

            return false;
        }

        #endregion
        #region GetWindow

        public static System.Windows.Window GetWindow(object control)
        {
            var dependencyObject = control as System.Windows.DependencyObject;
            if (dependencyObject != null)
                return System.Windows.Window.GetWindow(dependencyObject);
            return null;
        }

        #endregion
        #region RemoveControlFromWindow

        public static bool RemoveControlFromWindow(System.Windows.UIElement control, object owner)
        {
            System.Windows.Window windowOwner;
            System.Windows.DependencyObject dependencyObject;

            if (control == null)
                throw new System.ArgumentNullException("control");
            if (owner == null)
                return false;

            windowOwner = owner as System.Windows.Window;
            if (windowOwner != null)
                return RemoveChildFromParentControl(control, windowOwner);

            dependencyObject = owner as System.Windows.DependencyObject;
            if (dependencyObject != null)
                return RemoveChildFromParentControl(control, System.Windows.Window.GetWindow(dependencyObject));

            return false;
        }

        #endregion
        #region SetWindowOwner

        public static bool SetWindowOwner(System.Windows.Window window, object owner)
        {
            return SetWindowOwner(window, owner, System.Windows.WindowStartupLocation.CenterOwner, System.Windows.WindowStartupLocation.CenterScreen);
        }

        public static bool SetWindowOwner(System.Windows.Window window, object owner,
            System.Windows.WindowStartupLocation ownerLocation, System.Windows.WindowStartupLocation missingOwnerLocation)
        {
            System.Windows.Window windowOwner;
            System.Windows.DependencyObject dependencyObject;

            if (window == null)
                throw new System.ArgumentNullException("window");
            if (owner == null)
                return SetWindowOwnerAndLocation(window, null, ownerLocation, missingOwnerLocation);

            windowOwner = owner as System.Windows.Window;
            if (windowOwner != null)
                return SetWindowOwnerAndLocation(window, windowOwner, ownerLocation, missingOwnerLocation);

            dependencyObject = owner as System.Windows.DependencyObject;
            if (dependencyObject != null)
                return SetWindowOwnerAndLocation(window, System.Windows.Window.GetWindow(dependencyObject), ownerLocation, missingOwnerLocation);

            return SetWindowOwnerAndLocation(window, null, ownerLocation, missingOwnerLocation);
        }

        #endregion

        #endregion
        #region Private Members

        #region AddChildToParentControl

        private static bool AddChildToParentControl(System.Windows.UIElement child, object parent)
        {
            System.Windows.Controls.Panel panel;
            System.Windows.Controls.ItemsControl itemsControl;
            System.Windows.Controls.ContentControl contentControl;

            itemsControl = parent as System.Windows.Controls.ItemsControl;
            if (itemsControl != null)
            {
                itemsControl.Items.Add(child);
                return true;
            }

            panel = parent as System.Windows.Controls.Panel;
            if (panel != null)
            {
                panel.Children.Add(child);
                return true;
            }

            contentControl = parent as System.Windows.Controls.ContentControl;
            if (contentControl != null)
                return AddChildToParentControl(child, contentControl.Content);

            return false;
        }

        #endregion
        #region RemoveChildFromParentControl

        private static bool RemoveChildFromParentControl(System.Windows.UIElement child, object parent)
        {
            System.Windows.Controls.Panel panel;
            System.Windows.Controls.ItemsControl itemsControl;
            System.Windows.Controls.ContentControl contentControl;

            itemsControl = parent as System.Windows.Controls.ItemsControl;
            if (itemsControl != null)
            {
                itemsControl.Items.Remove(child);
                return true;
            }

            panel = parent as System.Windows.Controls.Panel;
            if (panel != null)
            {
                panel.Children.Remove(child);
                return true;
            }

            contentControl = parent as System.Windows.Controls.ContentControl;
            if (contentControl != null)
                return RemoveChildFromParentControl(child, contentControl.Content);

            return false;
        }

        #endregion
        #region SetWindowOwnerAndLocation

        private static bool SetWindowOwnerAndLocation(System.Windows.Window window, System.Windows.Window owner,
            System.Windows.WindowStartupLocation ownerLocation, System.Windows.WindowStartupLocation missingOwnerLocation)
        {
            if (owner != null)
            {
                window.Owner = owner;
                window.WindowStartupLocation = ownerLocation;
                return true;
            }
            else
            {
                window.WindowStartupLocation = missingOwnerLocation;
                return false;
            }
        }

        #endregion

        #endregion
    }
}
